
*************************************************************************
********** THIS .DO FILE FINALIZES THE CREATION OF THE DATASET **********
*************************************************************************

clear *
set maxvar 10000

use "${root}/data/processed/main_database.dta", clear
append using "${root}/data/processed/main_database_1996.dta"

* Create useful local macros to be used in the rest of the code
local time "_t_minus3 _t_minus2 _t_minus1 _t_0 _t_plus1 _t_plus2 _t_plus3 _t_plus4"
local vars "tot_exp tax_rev state_transf fed_transf current_exp capital_exp personnel investments tot_rev social_exp social_exp_p health education welfare nonsocial_exp housing transport other"
local measure "_share"

*************************
** Some house-cleaning **
*************************

* organize as a panel with a city as one unit of observation and a mayoral term as one time-period
destring mun_code, replace
xtset mun_code year

* Create year dummies
tab year, gen(yy)

*******************************************************************
** Create baseline sample indicator and prepare fiscal variables **
*******************************************************************

* Create baseline sample and extended baseline sample indicators
gen baseline_sample = 1
gen extended_baseline_sample=1

* Compute term-averages of fiscal variables, taking logs multiplied by 100 for per capita & constant prices values (so results can be interpreted as % differences)
foreach v of local vars {
	
	foreach s of local measure {
		
		* Create average fiscal variables over the term (levels)
		qui gen `v'_avg`s' = (`v'`s'_t_plus1 + `v'`s'_t_plus2 + `v'`s'_t_plus3 + `v'`s'_t_plus4)/4 /* average over the whole term */
		qui gen `v'_3y_avg`s' = (`v'`s'_t_plus2 + `v'`s'_t_plus3 + `v'`s'_t_plus4)/3 /* average excluding first year */
				
		* Update baseline sample indicator
		qui replace baseline_sample = 0  		  if (`v'_avg`s' == . | margin_mayor_left == .) 
		qui replace extended_baseline_sample = 0  if (`v'_avg`s' == . | margin_mayor_left == .) & (year>=2004 | "`v'"=="social_exp_p" |  margin_mayor_left == .)
				
		* Create average outcomes residualized on year and municipality fixed-effects
		qui areg `v'_avg`s' yy*, absorb(mun_code)
		qui predict res_`v'_avg`s', res
		
		qui areg `v'_3y_avg`s' yy*, absorb(mun_code)
		qui predict res_`v'_3y_avg`s', res
		
		}
	}

**********************************************************
* Save dataset file to be used in the empirical analysis *
**********************************************************

save "${root}/data/processed/extended_final_sample.dta", replace
